package com.wangzeqi.neihanduanzi.home.recommend;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageButton;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.wangzeqi.neihanduanzi.R;
import com.wangzeqi.neihanduanzi.base.GestureListener;
import com.wangzeqi.neihanduanzi.entity.AllEntity;
import com.wangzeqi.neihanduanzi.home.jokes.AdapterJokes;
import com.wangzeqi.neihanduanzi.singleton.QueueSingleton;

/**
 * Created by dllo on 16/1/8.
 */
public class FragmentRecommend extends Fragment {

    private Context context;
    private RecyclerView recyclerView;
    private AdapterRecommend adapter;
    private SwipeRefreshLayout refreshLayout;
    private AllEntity allEntity;
    private ImageButton button;
    private String url = "http://ic.snssdk.com/neihan/stream/mix/v1/?content_type=-101";

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recommend_fragment, null);
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_recommend);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setLongClickable(true);
        recyclerView.setOnTouchListener(new MyGestureListener(context));
        refreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_recommend);
        button = (ImageButton) view.findViewById(R.id.btn_recommend);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                recyclerView.scrollToPosition(0);
                starAnimation();
                refreshData();
            }
        });
        starAnimation();
        addData();
        return view;
    }

    private void  refreshData(){
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.setRefreshing(false);
                starAnimation();
                recyclerView.scrollToPosition(0);
                addData();
            }
        });
    }

    private void addData() {

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                allEntity = gson.fromJson(response, AllEntity.class);
                adapter = new AdapterRecommend(context, allEntity);
                stopAnimation();
                recyclerView.setAdapter(adapter);
                Toast.makeText(context, "刷新完成", Toast.LENGTH_SHORT).show();
                //   initDatabase();
                stopAnimation();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                stopAnimation();
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        QueueSingleton.getInstance().getQueue().add(request);
    }

    private void stopAnimation() {
        /** 设置旋转动画 */
        final RotateAnimation animation = new RotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        animation.setDuration(500);//设置动画持续时间
        animation.setRepeatCount(1000);
        button.setAnimation(animation);
        /** 结束动画 */
        animation.cancel();
    }

    private void starAnimation() {
        /** 设置旋转动画 */
        final RotateAnimation animation = new RotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF,
                0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        button.setAnimation(animation);
        animation.setDuration(500);//设置动画持续时间
        animation.setRepeatCount(1000);

        /** 开始动画 */
        animation.startNow();
    }

    private class MyGestureListener extends GestureListener {
        public MyGestureListener(Context context) {
            super(context);
        }

        @Override
        public boolean up() {
            recyclerView.bringToFront();
            return super.up();
        }

        @Override
        public boolean down() {
            button.bringToFront();
            return super.down();
        }
    }
}

